// pages/devapply/devapply.js
var comm = require("../../utils/comm.js")
var api = require('../../config/api.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value1: 1,
    money: 10000,

    defutadr: {},
    orderNotes: '',

  },

  handleChange1({
    detail
  }) {
    this.setData({
      value1: detail.value
    });
    if (this.data.value1 > 0) {
      var money = 10000;
      var mon = this.data.value1 * money;
      this.setData({
        money: mon
      })
    } else {
      this.setData({
        money: 0
      })
    }
  },

  adres: function(e) {
    wx.navigateTo({
      url: '/pages/adressmg/adressmg',
    })
  },

  realnameConfirm: function(e) {
    var that = this;
    that.setData({
      orderNotes: e.detail.value
    })
  },

  purchase: function(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认提交申请？',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })

        var adress = that.data.defutadr;
        var orderAddress = adress.provinceName + adress.cityName + adress.countyName + adress.detailInfo //订单地址
        var receiverCellPhone = adress.telNumber
        var receiverName = adress.userName
        var addressId = adress.id
        var orderNotes = that.data.orderNotes //订单备注
        var applyNum = that.data.value1 //订单数量
        var orderPrice = that.data.money //订单价格

        console.log('addressId=' + addressId)

        if (res.confirm) {
          console.log('for发生了submit事件，携带数据为：', e.detail.value);

          var reg = /^[0-9a-zA-Z]+$/;
          var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;

          if (orderAddress.lenght == 0) {
            wx.showToast({
              title: "请选择收货地址!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (applyNum == 0) {
            wx.showToast({
              title: "申请设备数量不能为0",
              icon: 'none',
              duration: 2000
            });
            return false;
          }
          wx.hideLoading();
          var userId = wx.getStorageSync('userId')
          wx.request({
            url: api.SalerDevAdd,
            data: {
              adress: adress,
              orderAddress: orderAddress,
              receiverCellPhone: receiverCellPhone,
              receiverName: receiverName,
              addressId: addressId,
              orderNotes: orderNotes,
              applyNum: applyNum,
              orderPrice: orderPrice,
              applyUserId: userId
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },

            success: function(res) {
              console.log(2);
              if (res.data.success) {
                var orderNum = res.data.obj;
                console.log('orderNum=' + orderNum);
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function(res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                    }
                  }
                });
                if (wx.getStorageSync('status') == 1) {
                  wx.navigateTo({
                    url: '/pages/deposit/deposit'
                  });
                }
                // if (wx.getStorageSync('status') == 2) {
                //   wx.navigateTo({
                //     url: '/pages/login/loginnew/login'
                //   });
                // }
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 3000
                });
              }


            },
            fail: function() {
              wx.hideLoading();
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 3000
              });
            },
          })

        } else if (res.cancel) {

          console.log('用户点击取消')
          wx.hideLoading();
        }
      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.setData({
      defutadr: wx.getStorageSync('defut')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})